﻿using System;
using System.Web.UI.WebControls;
using METU_CMT.Business;

namespace METU_CMT
{
    public partial class AssignReviewTeamScreen : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                BindAll( );
            }
        }

        private void BindAll( )
        {
            BindSubmissionTypes(  );
            BindTeams(  );
            BindReviewTeams(  );
            BindReviewTeamAssignments(  );
        }

        private void BindSubmissionTypes()
        {
            CourseInstructor _currentCourse = ( CourseInstructor ) EntityFactory.GetCurrentCourse(UserType.Instructor);
            ddlSubmissionTypes.DataSource = _currentCourse.GetSubmissionEvents();
            ddlSubmissionTypes.DataBind();
        }

        private void BindTeams()
        {
            CourseInstructor _currentCourse = (CourseInstructor)EntityFactory.GetCurrentCourse(UserType.Instructor);
            lstTeam.DataSource= _currentCourse.Teams;
            lstTeam.DataBind();
        }

        private void BindReviewTeams()
        {
            CourseInstructor _currentCourse = (CourseInstructor)EntityFactory.GetCurrentCourse(UserType.Instructor);
            lstReview.DataSource = _currentCourse.Teams;
            lstReview.DataBind();
        }

        protected void OnBtnAssignClicked(object sender, EventArgs e)
        {
            int _eventId = ddlSubmissionTypes.GetSelectedValue( );
            if(lstTeam.SelectedItem == null)
            {
                //TODO: Please choose a team.
                return;
            }

            if (lstReview.SelectedItem == null)
            {
                //TODO: Please choose a team.
                return;
            }
            int _reviewedTeamId = lstTeam.GetSelectedValue( );
            int _reviewerTeamId = lstReview.GetSelectedValue( );

            if(_reviewedTeamId == _reviewerTeamId )
            {
                //TODO: Message Box they are the same dude :)
                return;
            }
            ReviewTeam _reviewTeam = new ReviewTeam
                                         {
                                             Event = new Event( _eventId ),
                                             ReviewedTeam = new Team( _reviewedTeamId ),
                                             ReviewerTeam = new Team( _reviewerTeamId )
                                         };

            Instructor _instructor = (Instructor)EntityFactory.User;
            _instructor.AssignTeams( _reviewTeam);
            BindAll();
        }

        private void BindReviewTeamAssignments()
        {
            Course _currentCourse = EntityFactory.GetCurrentCourse(UserType.TA);
            dtgTeamAssignment.DataSource = _currentCourse.ReviewTeams;
            dtgTeamAssignment.DataBind();
        }

        protected void OnDtgTeamAssignmentRowCommand( object sender, GridViewCommandEventArgs e )
        {
            if(e.CommandName.CompareTo( "DeleteAssignment" ) == 0)
            {
                GridViewRow _row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
                int _reviewTeamId = CommonMethods.GetIdFromGridViewRow( _row );

                CourseTA _currentCourse = (CourseTA)EntityFactory.GetCurrentCourse(UserType.TA);
                _currentCourse.DeleteReviewer( _reviewTeamId );
                BindAll(  );
            }
        }
    }
}